/**
 * 
 */
package se.ricardo.gamesorgs.client.controllers;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JToolBar;

import se.ricardo.gamesorgs.client.GamesOrganizerGui;
import se.ricardo.gamesorgs.client.modes.games.GamesEditDialog;
import se.ricardo.gamesorgs.client.modes.games.GamesEditDialog.DialogMode;
import se.ricardo.gamesorgs.client.modes.games.GamesEditDialog.InputReadyEvent;
import se.ricardo.gamesorgs.client.modes.games.GamesEditDialog.InputReadyListener;


/**
 * Main Controller class for usage with the MVC design pattern.<br><br>
 * This controller handles the communication with the applications
 * JFrame.<br>
 * It also initiates the other controllers which handle the contents
 * of the frame.
 * <br>
 * 
 * @author Ricardo Ocaña Leal
 *
 */
public class GuiController {
	private GamesOrganizerGui gui;
	private GamesEditDialog dialog;
	private GamesController gController;
	
	/**
	 * Constructor that initiates the the other controllers.
	 * */
	public GuiController(GamesOrganizerGui gui) {
		this.gui = gui;
		
		gController = new GamesController();
		addToolBarListeners();
		gui.addWindowListener(new CustomWindowAdapter());
		
		gui.setContentMode(gController.getGamesView());
		
		dialog = new GamesEditDialog(gui, true);
		dialog.setCompanies(gController.getCompaniesArray());
		
		// Register listener for notifying that the user has
		// finished filling the dialog form, and everything
		// is in order.
		dialog.addInputReadyListener(new InputReadyListener() {
			
			@Override
			public void dialogInputReady(InputReadyEvent evt) {
				gController.addGameToDatabase(dialog.getRequest());
				dialog.setVisible(false);
				dialog.resetDialog();
			}
		});
		
		// Add ItemListener to get events from when the user changes
		// the company selection.
		dialog.addCompanyItemListener(new ItemListener() {
			
			@Override
			public void itemStateChanged(ItemEvent e) {
				gController.updateDialogSystems(dialog);
			}
		});
	}
	
	/**
	 * Called when the user has requested to shut down the
	 * application.<br>
	 * Runs through several procedures.
	 * */
	private void shutdownProcedure() {
		gController.shutdownProcedure();
	}
	
	/**
	 * Add listeners to the various components in the frames {@link JToolBar}.
	 * */
	private void addToolBarListeners() {
		JToolBar bar = gui.getToolBar();
		JButton add = (JButton)bar.getComponent(GamesOrganizerGui.ADD_BUTTON);
		JButton edit = (JButton)bar.getComponent(GamesOrganizerGui.EDIT_BUTTON);
		
		add.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				dialog.setTitle("Add A Game");
				dialog.setDialogMode(DialogMode.ADD);
				Point point = gui.getLocationOnScreen();
				Dimension frameDim = gui.getSize();
				int x = (point.x+frameDim.width)/2;
				int y = (point.y+frameDim.height)/2;
				
				gController.fillCurrentDialogValues(dialog);
				dialog.setVisible(x, y, true);
			}
		});
		
		edit.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				dialog.setTitle("Edit A Game");
				dialog.setDialogMode(DialogMode.EDIT);
				gController.fillSelectedGameValues(dialog);
				dialog.setVisible(true);
			}
		});
	}
	
	/**
	 * Adapter to catch events on the main application frame.
	 * */
	public class CustomWindowAdapter extends WindowAdapter {
		
		@Override
		public void windowClosing(WindowEvent e) {
			// The user wants to shut down the application.
			shutdownProcedure();
		}
	}
}
